package org.hibernate.criterion;

import java.sql.Types;

import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.TypedValue;
import org.hibernate.type.Type;

public class ExtendedExpression implements Criterion {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7803714710454129058L;

	private final Object value;
	private boolean ignoreCase;
	private final String op;
	private  SimpleFunction function;
	private  SQLFunction sqlFunction;

	protected ExtendedExpression(SimpleFunction function, Object value, String op) {
		this.function=function;
		this.value = value;
		this.op = op;
	}

	protected ExtendedExpression(SimpleFunction function, Object value, String op, boolean ignoreCase) {
		this.function=function;
		this.value = value;
		this.ignoreCase = ignoreCase;
		this.op = op;
	}

	protected ExtendedExpression(SQLFunction function, Object value, String op, boolean ignoreCase) {
		this.sqlFunction=function;
		this.value = value;
		this.ignoreCase = ignoreCase;
		this.op = op;
	}
	
	public ExtendedExpression ignoreCase() {
		ignoreCase = true;
		return this;
	}

	public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
	throws HibernateException {

		String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, function.getPropertyName());
		Type type = criteriaQuery.getTypeUsingProjection(criteria, function.getPropertyName());
		StringBuffer fragment = new StringBuffer();
		if (columns.length>1) fragment.append('(');
		SessionFactoryImplementor factory = criteriaQuery.getFactory();
		int[] sqlTypes = type.sqlTypes( factory );
		for ( int i=0; i<columns.length; i++ ) {
			boolean lower = ignoreCase && 
					( sqlTypes[i]==Types.VARCHAR || sqlTypes[i]==Types.CHAR );
			if (lower) {
				fragment.append( factory.getDialect().getLowercaseFunction() )
					.append('(');
			}
			fragment.append( function.toSqlString(criteria, criteriaQuery) );
			if (lower) fragment.append(')');
			fragment.append( getOp() ).append("?");
			if ( i<columns.length-1 ) fragment.append(" and ");
		}
		if (columns.length>1) fragment.append(')');
		return fragment.toString();

	}

	/**
	 * 
	 */
	public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
	throws HibernateException {
		Type mainFunctionType=function.getType(criteria, criteriaQuery);
		TypedValue type= criteriaQuery.getTypedValue(criteria, function.getPropertyName(), value) ;
		
		return new TypedValue[] { new TypedValue(mainFunctionType, value, EntityMode.POJO	) };
	}

	public String toString() {
		return function + getOp() + value;
	}

	protected final String getOp() {
		return op;
	}
	
	
	public static <T> T createObjectFromClass(Class<T> type) {
		T object = null;
		try {
			object = type.newInstance();
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return object;
	}
	

}
